package com.xxwy.ddu.security.code.config;

import com.xxwy.ddu.security.code.authentication.sms.SmsCodeAuthenticationFilter;
import com.xxwy.ddu.security.code.authentication.sms.SmsCodeAuthenticationProvider;
import com.xxwy.ddu.security.code.properties.SecurityProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.SecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.DefaultSecurityFilterChain;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.RememberMeServices;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.authentication.rememberme.AbstractRememberMeServices;
import org.springframework.security.web.authentication.rememberme.PersistentTokenBasedRememberMeServices;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;
import org.springframework.stereotype.Component;

import java.security.Security;

/**
 * @author xxwy
 * on 2018/8/22 0022
 */
@Component
public class SmsCodeAuthenticationConfig extends SecurityConfigurerAdapter<DefaultSecurityFilterChain,HttpSecurity> {

    @Autowired
    private SecurityProperties securityProperties;


    @Override
    public void configure(HttpSecurity http) throws Exception {
        SmsCodeAuthenticationFilter filter = new SmsCodeAuthenticationFilter();
        filter.setAuthenticationManager(http.getSharedObject(AuthenticationManager.class));
        filter.setAuthenticationSuccessHandler(authenticationSuccessHandler);
        filter.setAuthenticationFailureHandler(authenticationFailureHandler);
        //判断remember-me
        if (securityProperties.getBrowser().isRememberMe()&& persistentTokenRepository!= null) {
            filter.setRememberMeServices(new PersistentTokenBasedRememberMeServices("remember-me",userDetailsService,persistentTokenRepository));
        }
        SmsCodeAuthenticationProvider provider = new SmsCodeAuthenticationProvider();
        provider.setUserDetailsService(userDetailsService);
        // 将Provider添加到其中
        http.authenticationProvider(provider)
                //验证登录过滤器
                .addFilterAfter(filter, UsernamePasswordAuthenticationFilter.class);


    }

    /**
     * 用户认证Service
     *
     * @return
     */
    @Autowired
    UserDetailsService userDetailsService;

    /**
     * 认证失败Handler
     *
     * @return
     */
    @Autowired
    AuthenticationFailureHandler authenticationFailureHandler;

    /**
     * 认证成功Handler
     *
     * @return
     */
    @Autowired
    AuthenticationSuccessHandler authenticationSuccessHandler;

    @Autowired(required = false)
    PersistentTokenRepository persistentTokenRepository;


}